import configparser
import os

class Config:

    def __init__(self):
        self.config = configparser.ConfigParser()
        
        self.home_dir           = os.path.expanduser('~')
        self.config_dir_path    = self.home_dir + "/.config/roseng/"
        self.cache_dir_path     = self.config_dir_path + "cache"
        self.subs_path          = self.cache_dir_path + "/subscribes.txt"
        self.config_path        = self.config_dir_path + "config.ini"
        self.user_plugs_path    = self.config_dir_path + "/plugins"

        self.InstanceUrl            = 'http://127.0.0.1:5000'
        self.StandartVideoSpeed     = 1.00
        self.StandartVideoQuality   = 1080
        self.ShowVideoMenu          = True
        self.ReloadSubscribes       = False
        self.WaitForVideoEnd        = False
        self.VideoDownloadDirectory = '~/Downloads'

        self.config['Options'] = {
            'InstanceUrl'           : self.InstanceUrl,
            'StandartVideoSpeed'    : self.StandartVideoSpeed,
            'StandartVideoQuality'  : self.StandartVideoQuality,
            'ShowVideoMenu'         : self.ShowVideoMenu,
            'ReloadSubscribes'      : self.ReloadSubscribes,
            'WaitForVideoEnd'       : self.WaitForVideoEnd,
            'VideoDownloadDirectory': self.VideoDownloadDirectory
        }

        self.create_config_dir()
        self.create_plugs_dir()
        self.create_cache_dir()
        self.load_from_file()

        if not self.is_subs_exists(): 
            fp = open(self.subs_path, 'w'); fp.close()

    def is_config_exists(self):
        return os.path.exists(self.config_path)

    def is_subs_exists(self):
        return os.path.exists(self.subs_path)

    def create_config_dir(self):
        os.makedirs(self.config_dir_path, exist_ok=True)

    def create_plugs_dir(self):
        os.makedirs(self.user_plugs_path, exist_ok=True)

    def create_cache_dir(self):
        os.makedirs(self.cache_dir_path, exist_ok=True)

    def write_to_file(self):
        if self.is_config_exists(): return

        with open(self.config_path, 'w') as configfile:
            self.config.write(configfile)

    def load_from_file(self):
        if not self.is_config_exists(): return self.write_to_file()
        
        self.config.read(self.config_path)

        self.InstanceUrl            = self.config['Options']['InstanceUrl']
        self.StandartVideoSpeed     = self.config['Options']['StandartVideoSpeed']
        self.StandartVideoQuality   = self.config['Options']['StandartVideoQuality']
        self.ShowVideoMenu          = self.config['Options'].getboolean('ShowVideoMenu')
        self.ReloadSubscribes       = self.config['Options'].getboolean('ReloadSubscribes')
        self.WaitForVideoEnd        = self.config['Options'].getboolean('WaitForVideoEnd')
        self.VideoDownloadDirectory = self.config['Options']['VideoDownloadDirectory']

if __name__ == "__main__":
    cfg = Config()
    print(cfg.StandartVideoQuality)
